#include "Compass.h"

//------------------------------------------------------------------------------
//=== constructor ===//
CCompass::CCompass(core::rect<s32> rect, gui::IGUIEnvironment* env, gui::IGUIElement* parent)
      : gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, -1, rect)
{
   SetupQuadMesh( BodyMesh, 1.0f );
   SetupQuadMesh( NeedleMesh, 1.0f );

}

//------------------------------------------------------------------------------
//! set body of compass texture
void CCompass::SetCompassBodyTexture(video::ITexture* texture)
{
    BodyMesh.getMaterial().setTexture(0, texture);
}
//------------------------------------------------------------------------------
//! set needle of compass texture
void CCompass::SetCompassNeedleTexture(video::ITexture* texture)
{
    NeedleMesh.getMaterial().setTexture(0, texture);
}

//------------------------------------------------------------------------------
//! set compass heading
void CCompass::SetCompassHeading( f32 deg )
{
    Matrix.makeIdentity();
    Matrix.setRotationDegrees(core::vector3df(0, 0, deg));
}

//------------------------------------------------------------------------------
//! render the compass
void CCompass::draw()
{
    video::IVideoDriver* driver = Environment->getVideoDriver();
    if (! (driver && IsVisible))
        return;

    core::rect<s32> oldViewPort = driver->getViewPort();
    driver->setViewPort(getAbsolutePosition());

    // clear the projection matrix
    core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
    driver->setTransform(video::ETS_PROJECTION, core::matrix4());

    // clear the view matrix
    core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
    driver->setTransform(video::ETS_VIEW, core::matrix4());

    driver->setTransform(video::ETS_WORLD, Matrix);

   // draw compass body
    driver->setMaterial(BodyMesh.Material);
    driver->drawMeshBuffer(&BodyMesh);

    driver->setTransform(video::ETS_WORLD, core::matrix4());

   // draw the needle
   driver->setMaterial(NeedleMesh.Material);
    driver->drawMeshBuffer(&NeedleMesh);

    // restore view matrix
    driver->setTransform(video::ETS_VIEW, oldViewMat);

    // restore projection matrix
    driver->setTransform(video::ETS_PROJECTION, oldProjMat);



    // restore the view area
    driver->setViewPort(oldViewPort);
}
void CCompass::SetupQuadMesh( scene::SMeshBuffer &mesh, f32 f32Width )
{
   f32 f32HalfWidth = f32Width/2.0f;
    mesh.Vertices.set_used(4);
    mesh.Indices .set_used(6);

    video::SColor white(255, 255, 255, 255);

    mesh.Vertices[0] = video::S3DVertex(-f32HalfWidth, -f32HalfWidth, 0.f, 0.f, 0.f, 1.f, white, 0.f, 1.f);
    mesh.Vertices[1] = video::S3DVertex(-f32HalfWidth,  f32HalfWidth, 0.f, 0.f, 0.f, 1.f, white, 0.f, 0.f);
    mesh.Vertices[2] = video::S3DVertex( f32HalfWidth,  f32HalfWidth, 0.f, 0.f, 0.f, 1.f, white, 1.f, 0.f);
    mesh.Vertices[3] = video::S3DVertex( f32HalfWidth, -f32HalfWidth, 0.f, 0.f, 0.f, 1.f, white, 1.f, 1.f);

    mesh.Indices[0] = 0;
    mesh.Indices[1] = 1;
    mesh.Indices[2] = 2;
    mesh.Indices[3] = 2;
    mesh.Indices[4] = 3;
    mesh.Indices[5] = 0;

    mesh.getMaterial().Lighting = false;
    //mesh.getMaterial().BackfaceCulling = false;
    //mesh.getMaterial().Wireframe = true;
    mesh.getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
} 